<?php
// All seasons
$season = array('Winter'=>'http://register.rit.edu/courseSchedule/20102/',
				'Spring'=>'http://register.rit.edu/courseSchedule/20103/',
				'Summer'=>'http://register.rit.edu/courseSchedule/20104/',
				'Fall'=>'http://register.rit.edu/courseSchedule/20111/'
				);

// Connecting to WP db
include_once('../wp-config.php');
include_once('../wp-load.php');
include_once('../wp-includes/wp-db.php');

// Include Parser
include("LIB_parse.php");

// Show WP errors
$wpdb->show_errors();

// Remove timeout limit
ini_set('max_execution_time', 0);

// Start scrape
foreach($season as $key=>$val){
	save_col($val);
	echo "<span style='font-size: 40px;color:red;'>Done scraping $key!</span><br /><br />";
}

// Save Collegs
function save_col($url){
	// Make global
	global $wpdb;
	
	// Get content
	$data = file_get_contents($url);

	// Parse
	$excl = return_between($data, '<body>', '</body>', INCL);
	$mta = parse_array($data, '<td class="bodyTd">', '</td>');
	$remove_init  = return_between($mta[0], '<tr>','</td>', EXCL);
	$mta[0] = $remove_init.'</td>';
	
	foreach ($mta as $key){
		$url = return_between($key, '<a href="https://', '">', EXCL);
		$url = 'http://'.$url;
		$code = explode(" - ",return_between($key, '/">', '</a>', EXCL));
		$college_id = $code[0];
		$name = $code[1];
		$name = str_replace("'", "\'", $name);	// Escape single quote
		if(isset($code[2])){$name.=' - '.$code[2];}		// Concat any additiona arrays - some names have an additional dash ex. "Gaming - Ungrad"
		
		// See if in database first
		$in = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM colleges WHERE college_id='$college_id'"));
		
		// Save into DB
		if($in == 0){
			$wpdb->query("INSERT INTO colleges (`college_id`,`name`,`url`) VALUES ($college_id,'$name','$url')");
		}
		
		// See if URL is set and run query
		if(isset($url)){
			save_disc($url, $college_id);
		}
	}
}

function save_disc($url, $col_id){

	// Make global
	global $wpdb;
	
	// Get content
	$data = file_get_contents($url);

	// Parse
	$excl = return_between($data, '<body>', '</body>', INCL);
	$mta = parse_array($data, '<td class="bodyTd">', '</td>');
	$remove_init  = return_between($mta[0], '<tr>','</td>', EXCL);
	$mta[0] = $remove_init.'</td>';
	
	// Get content
	$data = file_get_contents($url);

	// Parse
	$excl = return_between($data, '<body>', '</body>', INCL);
	$mta = parse_array($data, '<td class="bodyTd">', '</td>');
	$remove_init  = return_between($mta[0], '<tr>','</td>', EXCL);
	$mta[0] = $remove_init.'</td>';
	
	foreach ($mta as $key){
		$url = return_between($key, '<a href="https://', '">', EXCL);
		$url = 'http://'.$url;
		$code = explode(" - ",return_between($key, '/">', '</a>', EXCL));
		$disc_id = $code[0];
		$name = $code[1];
		$name = str_replace("'", "\'", $name);	// Escape single quote
		if(isset($code[2])){$name.=' - '.$code[2];}		//Concat any additiona arrays - some names have an additional dash ex. "Gaming - Ungrad"
		
		// See if in database first
		$in = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM discipline WHERE disc_id='$disc_id'"));		
		
		// Save into DB
		if($in == 0){
			$wpdb->query("INSERT INTO discipline (`college_id`,`disc_id`,`name`,`url`) VALUES ($col_id,$disc_id,'$name','$url')");
		}
		// See if URL is set and run query
		if(isset($url)){
			save_courses($url, $disc_id);
		}
	}
	
}

/** SAVE COURSES **/
function save_courses($url, $disc_id){
	// Make global
	global $wpdb;
	
	// Get content
	$data = file_get_contents($url);

	// Parse
	$mta_light = parse_array($data, '<td class="bodyTdLight"><div align="left"><strong><a href="', '</a></strong></div></td>');
	$mta_dark = parse_array($data, '<td class="bodyTdDark"><div align="left"><strong><a href="', '</a></strong></div></td>');
	
	// Save each item - TdLight
	for($k=0;$k<count($mta_light);$k=$k+2){
		$course_num = strip_tags($mta_light[$k]);
		$course_name = strip_tags($mta_light[$k+1]);
		$course_name =  $wpdb->escape($course_name);
		
		// Is it in
		$in = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM courses WHERE course_id='$course_num'"));
		
		// Insert into DB
		if($in == 0){
			$wpdb->query("INSERT INTO courses (`disc_id`,`course_id`,`name`) VALUES ('$disc_id','$course_num','$course_name')");
			echo $course_num." ".$course_name. "<br />";
		}
	}

	// Save each item - TdDark
	for($k=0;$k<count($mta_dark);$k=$k+2){
		$course_num = strip_tags($mta_dark[$k]);
		$course_name = strip_tags($mta_dark[$k+1]);
		$course_name =  $wpdb->escape($course_name);
		
		// Is it in
		$in = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM courses WHERE course_id='$course_num'"));
		
		// Insert into DB
		if($in == 0){
			$wpdb->query("INSERT INTO courses (`disc_id`,`course_id`,`name`) VALUES ('$disc_id','$course_num','$course_name')");
			echo $course_num." ".$course_name. "<br />";
		}
	}
}

// Done
echo "<span style='font-size: 40px;color:red;'>Done scraping all!</span><br /><br />";